package vn.edu.saigontech.ncms.web.englishtest;

import java.util.Collection;

import vn.edu.saigontech.ncms.model.englishtest.Candidate;
import vn.edu.saigontech.ncms.model.englishtest.TestRegister;
import vn.edu.saigontech.ncms.model.enumeration.Currency;
import vn.edu.saigontech.ncms.model.system.Money;
import vn.edu.saigontech.ncms.service.BankAccountService;
import vn.edu.saigontech.ncms.service.CandidateService;
import vn.edu.saigontech.ncms.service.CandidateTypeService;
import vn.edu.saigontech.ncms.service.TestRegisterService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class RegisterAction extends BaseAction {
	
	private Long id;
	private Long candidateId;
	private String registeredDate;
	private Double fee;
	private String number;
	private Long bankAccountId;
	private Long consultantId;
	
	public String showAdd() {
		REQUEST().setAttribute("candidate", CandidateService.INSTANCE.findById(this.candidateId));
		REQUEST().setAttribute("bankAccounts", BankAccountService.INSTANCE.findAll());
		REQUEST().setAttribute("consultants", UserService.INSTANCE.findByRole("Consulting"));
		return SUCCESS;
	}

	public String add() {
		TestRegister register = new TestRegister();
		register.setCandidate(CandidateService.INSTANCE.findById(this.candidateId));
		register.setDate(DateUtil.fromString(this.registeredDate, this.i18n("format.date")));
		register.setBankAccount(BankAccountService.INSTANCE.findById(this.bankAccountId));
		register.setReceiptNumber(this.number);
		register.setFee(new Money(this.fee, Currency.VND));
		register.setCashier(UserService.INSTANCE.findById(this.consultantId));
		register = TestRegisterService.INSTANCE.persist(register);
		
		this.addLoggingDetail(BaseAction.ADD, "logging.registertest.add", register.getReceiptNumber(), register.getCandidate().getFullName());
		return SUCCESS;
	}
	
	public String list() {
		REQUEST().setAttribute("registers", TestRegisterService.INSTANCE.findByCandidate(CandidateService.INSTANCE.findById(this.candidateId)));
		REQUEST().setAttribute("candidateId", this.candidateId);
		return SUCCESS;
	}
	
	public String showEdit() {
		REQUEST().setAttribute("bankAccounts", BankAccountService.INSTANCE.findAll());
		REQUEST().setAttribute("consultants", UserService.INSTANCE.findAllByRole("Consulting"));
		TestRegister register = TestRegisterService.INSTANCE.findById(this.id);
		REQUEST().setAttribute("register", register);
		REQUEST().setAttribute("candidate", register.getCandidate());
		return SUCCESS;
	}
	
	public String edit() {
		TestRegister register = TestRegisterService.INSTANCE.findById(this.id);
		register.setDate(DateUtil.fromString(this.registeredDate, this.i18n("format.date")));
		register.setBankAccount(BankAccountService.INSTANCE.findById(this.bankAccountId));
		register.setReceiptNumber(this.number);
		register.setFee(new Money(this.fee, Currency.VND));
		register.setCashier(UserService.INSTANCE.findById(this.consultantId));
		register = TestRegisterService.INSTANCE.persist(register);
		this.addLoggingDetail(BaseAction.ADD, "logging.registertest.edit", register.getReceiptNumber(), register.getCandidate().getFullName());
		
		return SUCCESS;
	}
	
	public String delete() {
		TestRegister register = TestRegisterService.INSTANCE.findById(this.id);
		String fullName = register.getCandidate().getFullName();
		String receiptNumber = register.getReceiptNumber();
		if(TestRegisterService.INSTANCE.deleteById(this.id)) {
			this.setKeyMessage("confirm.deleteSuccessfully", "register");
			this.addLoggingDetail(BaseAction.DELETE, "logging.register.delete", fullName, receiptNumber);
		}
		else {
			this.setKeyMessage("confirm.deleteUnsuccessfully", "register");
		}
		return SUCCESS;
	}
	
	public String deleteSearch() {
//		if(!Checker.isNull(this.ids)) {
//			for (Long each : this.ids) {
//				Candidate candidate = CandidateService.INSTANCE.findById(each);
//				String name = candidate.getFullName();
//				if(CandidateService.INSTANCE.deleteById(each)) {
//					this.addLoggingDetail(BaseAction.DELETE, "logging.candidate.deleteSearch", name);
//				}
//			}
//		}
		return SUCCESS;
	}
	
	public String showCacheSearchResult() {
		Candidate candidate = (Candidate) SESSION().getAttribute("searchStudent");
		if(Checker.isNull(candidate))
			candidate = new Candidate();
		Collection<Candidate> candidates = CandidateService.INSTANCE.find(candidate);
		REQUEST().setAttribute("candidates", candidates);
		return SUCCESS;
	}
	
	public String showSearch() {
		REQUEST().setAttribute("types", CandidateTypeService.INSTANCE.findAll());
		REQUEST().setAttribute("consultants", UserService.INSTANCE.findAllByRole("Consulting"));
		return SUCCESS;
	}
	
	public String search() {
//		Candidate candidate = new Candidate();
//		candidate.setCandidateCode(this.candidateCode);
//		candidate.setFirstName(this.firstName);
//		candidate.setLastName(this.lastName);
//		candidate.setNickName(this.nickName);
//		candidate.setGender(Gender.create(this.gender));
//		candidate.setDateOfBirth(DateUtil.fromString(this.dateOfBirth, this.i18n("format.date")));
//		candidate.setAddress(this.address);
//		candidate.setPhoneNumber(this.phoneNumber);
//		candidate.setEmail(this.email);
//		candidate.setType(CandidateTypeService.INSTANCE.findById(this.typeId));
//		candidate.setComment(this.comment);
//		candidate.setRegisteredDate(DateUtil.fromString(this.registeredDate, this.i18n("format.date")));
//		candidate.setConsultant(UserService.INSTANCE.findById(this.consultantId));
		
//		SESSION().setAttribute("searchCandidate", candidate);
		
		this.addLoggingDetail(BaseAction.SEARCH, "logging.candidate.search");
		return SUCCESS;
	}
	
//	public String showHistory() {
//		Student student = StudentService.INSTANCE.findById(this.id);
//		Level lastLevel = student.getLastLevel();
//		Level nextLevel = LevelService.INSTANCE.findNextLevel(lastLevel);
//		REQUEST().setAttribute("student", student);
//		REQUEST().setAttribute("nextLevel", nextLevel);
//		return SUCCESS;
//	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getRegisteredDate() {
		return registeredDate;
	}

	public void setRegisteredDate(String registeredDate) {
		this.registeredDate = registeredDate;
	}

	public Long getConsultantId() {
		return consultantId;
	}

	public void setConsultantId(Long consultantId) {
		this.consultantId = consultantId;
	}

	public Long getCandidateId() {
		return candidateId;
	}

	public void setCandidateId(Long candidateId) {
		this.candidateId = candidateId;
	}

	public Double getFee() {
		return fee;
	}

	public void setFee(Double fee) {
		this.fee = fee;
	}

	public Long getBankAccountId() {
		return bankAccountId;
	}

	public void setBankAccountId(Long bankId) {
		this.bankAccountId = bankId;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}
}